<?php
class App {

    public function __construct() {
        $this->_define();
        $this->init();
    }

    public function run( $path ) {
        if ( !is_readable( APP_PATH . "/system/config.php" ) ) {
            $path = "install";
        } else {
            require( APP_PATH . "/system/config.php" );
            $this->init_config();
        }
        $this->exec( $path );
    }

    private function exec( $path ) {

        // 构造执行路由
        $__controller = R( 'c', 'main' );
        $__action = R( 'a', 'index' );

        if ( $url = C( 'UrlRewrite' )->set_url() ) $__controller = $url;

        // 对将要访问的控制器类进行实例化
        $handle_controller = C( $__controller, null, APP_PATH . "/system/{$path}/{$__controller}.php" );

        // 调用控制器出错将调用路由错误处理函数
        if ( !is_object( $handle_controller ) || !method_exists( $handle_controller, $__action ) ) {
            Error( "路由错误，请检查控制器目录下是否存在该控制器{$__controller}/动作{$__action}。" );
        }

        // 路由并执行用户代码
        $handle_controller->$__action( $__controller, $__action );

        // 对路由进行后续相关操作
    }

    private function init() {

        // 载入核心函数库
        require( APP_PATH . "/system/core/Function.php" );
        require( APP_PATH . "/system/core/Db.php" );
        require( APP_PATH . "/system/core/Model.php" );
        require( APP_PATH . "/system/core/Kernel.php" );
        require( APP_PATH . "/system/core/Front.php" );

        if ( basename( $_SERVER[ 'SCRIPT_NAME' ] ) === basename( $_SERVER[ 'SCRIPT_FILENAME' ] ) ) {
            $GLOBALS[ "url_path_base" ] = $_SERVER[ 'SCRIPT_NAME' ];
        } elseif ( basename( $_SERVER[ 'PHP_SELF' ] ) === basename( $_SERVER[ 'SCRIPT_FILENAME' ] ) ) {
            $GLOBALS[ "url_path_base" ] = $_SERVER[ 'PHP_SELF' ];
        } elseif ( isset( $_SERVER[ 'ORIG_SCRIPT_NAME' ] ) && basename( $_SERVER[ 'ORIG_SCRIPT_NAME' ] ) === basename( $_SERVER[ 'SCRIPT_FILENAME' ] ) ) {
            $GLOBALS[ "url_path_base" ] = $_SERVER[ 'ORIG_SCRIPT_NAME' ];
        }

        header( 'Content-Type: text/html; charset=UTF-8' );
        date_default_timezone_set( 'PRC' );
        ob_start();
        session_start();
        error_reporting( E_ALL & ~E_NOTICE & ~E_WARNING );

        if ( substr( PHP_VERSION, 0, 3 ) < 5.2 ) exit( "本系统运行环境要求PHP 5.2！" );
    }

    private function init_config() {
        if ( isset( $GLOBALS[ 'setting' ] ) ) return;
        $setting = C( 'Model' )->Cache()->get( 'setting' );
        @date_default_timezone_set( $setting[ 'timezone' ] ? $setting[ 'timezone' ] : 'PRC' );
        $setting[ 'weburl' ] = !empty( $setting[ 'weburl' ] ) ? $setting[ 'weburl' ] : S();
        $setting[ 'uptype' ] = empty( $setting[ 'uptype' ] ) ? '*.*' : $setting[ 'uptype' ];
        $setting[ 'upsize' ] = ( empty( $setting[ 'upsize' ] ) ? intval( ini_get( 'upload_max_filesize' ) ) : $setting[ 'upsize' ] );
        $setting[ 'rewrite' ] = unserialize( $setting[ 'rewrite' ] );
        $GLOBALS[ 'setting' ] = $setting;
    }

    public function load( $file ) {
        import( $file . ".php" );
    }

    private function _define() {
        define( 'APP_CACHE', APP_PATH . "/content/cache/" );
        define( 'APP_TPL', APP_PATH . "/content/tpl/" );
        define( 'APP_PLUGIN', APP_PATH . "/system/plugin/" );
    }
}